package com.zygh.policeasset.utils;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/utils/ImageToBase64Util.class */
public class ImageToBase64Util {
    public static String toBase64(String imageUrl) throws MalformedURLException {
        String urlBase64 = null;
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(imageUrl).openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            if (conn.getResponseCode() == 200) {
                InputStream inStream = conn.getInputStream();
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                while (true) {
                    int len = inStream.read(buffer);
                    if (len != -1) {
                        outStream.write(buffer, 0, len);
                    } else {
                        inStream.close();
                        urlBase64 = new BASE64Encoder().encode(outStream.toByteArray());
                        return urlBase64;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return urlBase64;
    }

    public static String urlTochar(String url) throws Exception {
        System.out.println("原始URL：" + url);
        if (url.contains("（")) {
            url = url.replaceAll("（", URLEncoder.encode("（", "utf-8"));
        }
        if (url.contains("）")) {
            url = url.replaceAll("）", URLEncoder.encode("）", "utf-8"));
        }
        String string = new Scanner(url).nextLine();
        String resultURL = "";
        for (int i = 0; i < string.length(); i++) {
            char charAt = string.charAt(i);
            if (isChineseChar(charAt)) {
                resultURL = resultURL + URLEncoder.encode(charAt + "", "UTF-8");
            } else {
                resultURL = resultURL + charAt;
            }
        }
        return resultURL;
    }

    public static boolean isChineseChar(char c) {
        return String.valueOf(c).matches("[一-龥]");
    }

    public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException {
        OutputStream out = null;
        if (imgData == null) {
            return false;
        }
        try {
            BASE64Decoder decoder = new BASE64Decoder();

            try {
                out = new FileOutputStream(imgFilePath);
                byte[] b = decoder.decodeBuffer(imgData);
                for (int i = 0; i < b.length; i++) {
                    if (b[i] < 0) {
                        b[i] = (byte) (b[i] + 256);
                    }
                }
                out.write(b);
                out.flush();
                out.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                out.flush();
                out.close();
                return true;
            } catch (IOException e2) {
                e2.printStackTrace();
                out.flush();
                out.close();
                return true;
            }
        } catch (Throwable th) {
            out.flush();
            out.close();
            return true;
        }
    }
}